<!--
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2022-06-02 18:37:34
 * @LastEditors: Sam
 * @LastEditTime: 2022-06-14 11:08:33
-->

<template>
  <el-form ref="form" :model="data" :rules="rules" label-width="100px">
                    <el-form-item label="关键字" prop="keyword">
                        <el-input v-model.trim="data.keyword" placeholder="请填写关键字" />
                    </el-form-item>
                      <el-form-item label="消息类型">
                      <el-radio-group v-model="data.msg_type">
                        <el-radio :label="1">文本</el-radio>
                        <el-radio :label="2">图片</el-radio>
                        <el-radio :label="3">图文</el-radio>
                        <el-radio :label="4">小程序</el-radio>
                      </el-radio-group>
                    </el-form-item>
                    <el-form-item label="回复内容" prop="content" v-if="data.msg_type == 1">
                        <el-input  type="textarea" :rows="2" v-model.trim="data.content" placeholder="请填写回复内容" />
                    </el-form-item>
                     <el-form-item label="图片" prop="imgUrl" v-if="data.msg_type == 2">
                        <el-upload
                        :action="$store.state.settings.media_upload_url"
                        :headers ="headerObj"
                        :limit=1
                        list-type="picture-card"
                        :file-list="data.imgUrlfileList"
                        accept="image/png, image/jpeg, image/gif, image/jpg"
                        :on-success="handleimgUrlSuccess"
                        :before-upload="beforeimgUrlUpload"
                        :on-exceed="handleimgUrlExceed"
                        :on-preview="handleimgUrlPreview"
                        :on-remove="handleimgUrlRemove">
                        <i class="el-icon-plus"></i>
                        </el-upload>
                       
                    </el-form-item>
                    <el-form-item label="消息标题" prop="title" v-if="data.msg_type == 3">
                        <el-input v-model.trim="data.title" placeholder="请填写消息标题" />
                    </el-form-item>

                    <el-form-item label="图文链接消息" prop="description" v-if="data.msg_type == 3">
                        <el-input v-model.trim="data.description" placeholder="请填写图文链接消息" />
                    </el-form-item>
                    <el-form-item label="跳转的链接" prop="url" v-if="data.msg_type == 3">
                        <el-input v-model.trim="data.url" placeholder="请填写图文跳转的链接" />
                    </el-form-item>

                    <el-form-item label="图文链接封面" prop="thumb_url" v-if="data.msg_type == 3">
                        <el-upload
                        :action="$store.state.settings.file_upload_url"
                        :headers ="headerObj"
                        :limit=1
                        list-type="picture-card"
                        :file-list="data.thumbUrlfileList"
                        accept="image/png, image/jpeg, image/gif, image/jpg"
                        :on-success="handlethumbUrlSuccess"
                        :before-upload="beforethumbUrlUpload"
                        :on-exceed="handlethumbUrlExceed"
                        :on-preview="handlethumbUrlPreview"
                        :on-remove="handlethumbUrlRemove">
                        <i class="el-icon-plus"></i>
                        </el-upload>
                       
                    </el-form-item>
                   <el-form-item label="消息标题" prop="title" v-if="data.msg_type == 4">
                        <el-input v-model.trim="data.title" placeholder="请填写消息标题" />
                    </el-form-item>
                     <el-form-item label="小程序的页面路径" prop="pagepath" v-if="data.msg_type == 4">
                        <el-input v-model.trim="data.pagepath" placeholder="小程序的页面路径" />
                    </el-form-item>
                     <el-form-item label="消息封面" prop="thumb_media_id" v-if="data.msg_type == 4">
                        <el-upload
                        :action="$store.state.settings.media_upload_url"
                        :headers ="headerObj"
                        :limit=1
                        list-type="picture-card"
                        :file-list="data.imgUrlfileList"
                        accept="image/png, image/jpeg, image/gif, image/jpg"
                        :on-success="handlethumbMediaSuccess"
                        :before-upload="beforeimgUrlUpload"
                        :on-exceed="handleimgUrlExceed"
                        :on-preview="handleimgUrlPreview"
                        :on-remove="handleimgUrlRemove">
                        <i class="el-icon-plus"></i>
                        </el-upload>
                       
                    </el-form-item>
    <el-dialog :visible.sync="dialogVisible" width="30%" append-to-body>
              <img width="100%" :src="previewUrl" alt="">
    </el-dialog>  
    <el-form-item>
      <el-button type="primary" :disabled="$store.state.settings.ajaxButton" @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { create } from '@/api/customer'
import { getToken } from '@/utils/auth'


export default {
  name: 'customerCreate',
  components: { },
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      loading:false,
      dialogVisible:false,
      previewUrl:'',
      headerObj: {
            'Authorization': 'Bearer ' + getToken()
        },
      rules: {
                    keyword: [ 
                { required: true, message: "请输入关键字", trigger: "blur" }
            ], 
            msg_type: [ 
                { required: true, message: "请选择回复类型", trigger: "blur" }
            ], 

      },
      data: {
            keyword:'',
            content:'',
            msg_type:1,
            imgUrlfileList:[],
            thumbUrl:'',
            thumbUrlfileList:[],
            imgUrl:'',
            media_id:'',
            thumb_media_id:'',
      }
    }
  },
  created() {
  },
  methods: {
    
            handleimgUrlExceed(){ 
                this.$message.error('只能上传一张图片');
             },
            handleimgUrlPreview(file){
                 this.dialogVisible = true;
                 this.previewUrl = file.url;
             },
            handleimgUrlRemove(){
                 this.data.imgUrl = '';
             },
             handleimgUrlSuccess(response, file, fileList) {
                this.$store.commit('settings/SET_AJAX_BUTTON', false);
                if(response.code ==1){
                  this.data.media_id= response.data.media_id;
                }else{
                   this.$message.error('上传服务器失败：'+response.msg+':'+response.data.errmsg);
                }
                
            },
            handlethumbMediaSuccess(response, file, fileList) {
                this.$store.commit('settings/SET_AJAX_BUTTON', false);
                if(response.code==1){
                  this.data.thumb_media_id= response.data.media_id;
                }else{
                   this.$message.error('上传服务器失败：'+response.msg+':'+response.data.errmsg);
                }
                
            },
        
             beforeimgUrlUpload(file) {
    
                this.$store.commit('settings/SET_AJAX_BUTTON', true);
                const isJPG = file.type === "image/jpeg";
                const isPNG = file.type === "image/png";
                const isLt5M = file.size / 1024 / 1024 < 5;
                if (!isJPG && !isPNG) {
                this.$message.error("上传头像图片只能是 JPG 、PNG格式!");
                }
                if (!isLt5M) {
                this.$message.error("上传头像图片大小不能超过 5MB!");
                }
       
            },

            handlethumbUrlExceed(){ 
                this.$message.error('只能上传一张图片');
             },
            handlethumbUrlPreview(file){
                 this.dialogVisible = true;
                 this.previewUrl = file.url;
             },
            handlethumbUrlRemove(){
                 this.data.thumbUrl = '';
             },
             handlethumbUrlSuccess(response, file, fileList) {
                this.$store.commit('settings/SET_AJAX_BUTTON', false);
                this.data.thumbUrl = response.data.src;
                
            },
        
             beforethumbUrlUpload(file) {
    
                this.$store.commit('settings/SET_AJAX_BUTTON', true);
                const isJPG = file.type === "image/jpeg";
                const isPNG = file.type === "image/png";
                const isLt5M = file.size / 1024 / 1024 < 5;
                if (!isJPG && !isPNG) {
                this.$message.error("上传头像图片只能是 JPG 、PNG格式!");
                }
                if (!isLt5M) {
                this.$message.error("上传头像图片大小不能超过 5MB!");
                }
       
            },
    
    submit() {
 
      const thiz = this

      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }
        create(this.data).then(response => {
          this.$store.commit('settings/SET_AJAX_BUTTON', true);
          this.$message({
            message: '添加成功',
            type: 'success',
            duration: 1500,
            onClose() {
              if (thiz.$refs.form !== undefined) {
                thiz.$refs.form.resetFields()
              }
              thiz.item.dialogVisible = false
            }
          })
        })
      })
    }
  }
}
</script>
